from flask import render_template, abort, request, send_from_directory, jsonify, flash, redirect, url_for
from flask_login import login_required, current_user
from sqlalchemy.sql.expression import func
from app import app, db, modles
from app.utils.response_result import *

# 摘录

@app.route('/extracts', methods=['GET'])
def extracts():
    """
    :return:
    """
    extracts = modles.Extract.query.all()

    return jsonify(code=1000, msg='ok',
                   data=[i.extract_vo for i in extracts])


@app.route('/extracts/<int:extract_id>', methods=['GET'])
def get_extracts(extract_id):
    """
    :param extract_id:
    :return:
    """
    extract = modles.Extract.query.filter_by(id=extract_id).first()

    return jsonify(code=1000, data=extract.extract_vo)


@app.route('/extracts', methods=['POST'])
@login_required
def post_extracts():
    """创建一个分类
    :return:
    """
    name = request.form['name']

    if not name or len(name) > 25:
        flash('输入无效')
        return redirect(url_for('post_sort'))
    sort = modles.WebsiteSort(name=name)
    db.session.add(sort)
    db.session.commit()
    # return flask_ext.my_redirect('sorts', request)
    return redirect(url_for('sorts'))


@app.route('/extracts/<int:extract_id>', methods=['POST', 'PUT', 'DELETE'])
@login_required
def put_extracts(extract_id):
    """根据 id 更新/删除 一个分类

    :param extract_id:
    :return:
    """
    _method = request.form['_method']
    sort = modles.WebsiteSort.query.filter_by(id=extract_id).first()

    # 更新
    if _method == 'PUT':
        name = request.form['name']
        if not name or len(name) > 25:
            flash('输入无效')
            return redirect(url_for('get_sort', sort_id=1))

        sort.name = name  # 更新名称
        db.session.commit()  # 提交数据库会话
        return redirect(url_for('sorts'))
    # 删除
    elif _method == 'DELETE':
        flash('删除成功')
        db.session.delete(sort)
        db.session.commit()
        return redirect(url_for('sorts'))
    else:
        flash('访问无效')
        return redirect(url_for('sorts'))


# @app.route('/sorts/<int:sort_id>', methods=['DELETE'])
# def delete_sort(sort_id):
"""根据 id 删除
"""


@app.route('/extracts/random/<int:type>', methods=['GET'])
def get_extracts_random_type(type):
    """随机获取
    :param type:
    :return:
    """

    # 获取随机行数据
    extract = modles.Extract.query.filter_by(is_delete=0, type_id=type).order_by(func.random()).limit(1).first()

    if extract:
        return result_ok(data=extract.extract_vo)
    else:
        return result_no_data()


@app.route('/api/extracts/random', methods=['GET'])
def get_extracts_random():
    logic = modles.Extract.query.filter_by(is_delete=0, type_id=1001).order_by(func.random()).limit(1).first()
    quotation = modles.Extract.query.filter_by(is_delete=0, type_id=1002).order_by(func.random()).limit(1).first()
    data = {}
    if logic:
        data['logic'] = logic.extract_vo
    if quotation:
        data['quotation'] = quotation.extract_vo

    return result_ok(data=data)
